// src/stores/useUserStore.js
import { defineStore } from 'pinia';

export const useUserStore = defineStore('user', {
  state: () => ({
    userInfo: null,
    token: localStorage.getItem('token') || null,
  }),
  actions: {
    login(credentials) {
      // 模拟登录逻辑
      return new Promise((resolve) => {
        this.token = 'mock-token';
        this.userInfo = { id: 1, username: credentials.username };
        localStorage.setItem('token', this.token);
        resolve();
      });
    },
    register(user) {
      // 模拟注册逻辑
      return new Promise((resolve) => {
        this.token = 'mock-token';
        this.userInfo = { id: 1, username: user.username };
        localStorage.setItem('token', this.token);
        resolve();
      });
    },
    logout() {
      this.token = null;
      this.userInfo = null;
      localStorage.removeItem('token');
    },
  },
  getters: {
    isLoggedIn: (state) => !!state.token,
    userInfo: (state) => state.userInfo,
  },
});